package com.example.springboot.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created by tang on 2018/3/7.
 */
@Aspect
@Component
public class TestAop {


    @Pointcut("execution(public * com.example.springboot.controller.TestAopController.hello(..))")
    public void executeService() {
    }


    @Before("executeService()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("before " + joinPoint.getSignature().getName());
    }


    @AfterReturning(value = "executeService()", returning = "ret")
    public void doAfterReturing(JoinPoint joinPoint, Object ret) {
        System.out.println("method:" + joinPoint.getSignature().getName() + " return:" + ret);
    }


    @After("executeService()")
    public void doAfter(JoinPoint joinPoint) {
        System.out.println("after " + joinPoint.getSignature().getName());
    }


    @AfterThrowing(value = "execution(public * com.example.springboot.controller.TestAopController.error(..))", throwing = "ex")
    public void doAfterThrowing(JoinPoint joinPoint, Exception ex) {
        System.out.println("method:" + joinPoint.getSignature().getName() + " throwing:" + ex.getMessage());
    }

    @Around(value = "execution(public * com.example.springboot.controller.TestAopController.testAround(..))")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) {
        try {
            System.out.println("before proceed!");
            Object object = proceedingJoinPoint.proceed();
            System.out.println("after proceed!");

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
         return "hello";
    }
}
